package com.zx.www.volunteersystem.controller;

import com.zx.www.volunteersystem.pojo.ActivityEnroll;
import com.zx.www.volunteersystem.mapper.activityEnrollMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/enroll")
@Tag(name = "活动报名管理接口")
@CrossOrigin(origins = "*")

public class activityEnrollController {

    //http://localhost:8181/api/enroll

    @Autowired
    private activityEnrollMapper activityEnrollMapper;

    // 根路径访问
    @GetMapping("/")
    @Operation(summary = "活动报名管理首页")
    public String index() {
        return "Activity Enrollment Management System";
    }

    // 新增报名记录
    @PostMapping("/add")
    @Operation(summary = "新增报名记录")
    public String addEnroll(@RequestBody ActivityEnroll enroll) {
        enroll.setEnroll_time(new Date());
        enroll.setCreate_time(LocalDateTime.now());
        enroll.setUpdate_time(LocalDateTime.now());

        int result = activityEnrollMapper.insertActivityEnroll(enroll);
        return result > 0 ? "报名成功" : "报名失败";
    }

    // 根据ID查询报名记录
    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询报名记录")
    public ActivityEnroll getEnrollById(@PathVariable Integer id) {
        return activityEnrollMapper.selectById(id);
    }

    // 根据活动ID查询所有报名记录
    @GetMapping("/list/activity/{activityId}")
    @Operation(summary = "根据活动ID查询所有报名记录")
    public List<ActivityEnroll> getEnrollByActivityId(@PathVariable Integer activityId) {
        return activityEnrollMapper.selectByActivityId(activityId);
    }

    // 根据用户ID查询报名记录
    @GetMapping("/list/user/{userId}")
    @Operation(summary = "根据用户ID查询报名记录")
    public List<ActivityEnroll> getEnrollByUserId(@PathVariable Long userId) {
        return activityEnrollMapper.selectByUserId(userId);
    }

    // 根据状态查询报名记录
    @GetMapping("/list/status/{status}")
    @Operation(summary = "根据状态查询报名记录")
    public List<ActivityEnroll> getEnrollByStatus(@PathVariable Integer status) {
        return activityEnrollMapper.selectByStatus(status);
    }

    // 更新报名状态
    @PutMapping("/update/{id}")
    @Operation(description = "update报名 state")
    public String updateEnroll(@PathVariable Integer id, @RequestBody ActivityEnroll enroll) {
        enroll.setEnroll_id(id);
        enroll.setUpdate_time(LocalDateTime.now());

        int result = activityEnrollMapper.updateActivityEnroll(enroll);
        return result > 0 ? "更新成功" : "更新失败";
    }

    // 删除报名记录
    @GetMapping("/delete/{id}")
    @Operation(summary = "删除报名记录")
    public String deleteEnroll(@PathVariable Integer id) {
        int result = activityEnrollMapper.deleteById(id);
        return result > 0 ? "删除成功" : "删除失败";
    }

    // 查询所有报名记录
    @GetMapping("/list")
    @Operation(summary = "查询所有报名记录")
    public List<ActivityEnroll> getAllEnrolls() {
        // 实现具体的查询逻辑
        return activityEnrollMapper.selectAll();
    }

    // 添加在Controller类中
    @GetMapping("/test")
    public String test() {
        return "Controller访问正常 - " + new Date();
    }

    @GetMapping("/test-mapper")
    public String testMapper() {
        try {
            // 测试数据库连接和Mapper
            int count = activityEnrollMapper.selectAll().size();
            return "数据库连接正常，共有 " + count + " 条报名记录";
        } catch (Exception e) {
            return "数据库连接异常: " + e.getMessage();
        }
    }

    @GetMapping("/check-bean")
    public String checkBean() {
        return "Mapper Bean状态: " + (activityEnrollMapper != null ? "已注入" : "未注入");
    }
}
